<?php
namespace PsiFramework\Classes\Util\Manager;

import("Classes.Util.Parameters.DAO.ParametersDAO");
import("Classes.Util.Parameters.DAO.ActionMappingDAO");
import("Classes.Util.Parameters.Action.ActionMapping");
import("Classes.Util.Parameters.ApplicationsParameters");
import("Classes.Util.Parameters.DatabaseParameters");
import("Classes.Util.Parameters.ObjectParameters");
import("Classes.Util.Manager.ParametersManager");
import("Classes.Util.HTTP.Session");
import("Classes.Util.Log.Logger");

use \PsiFramework\Classes\Util\Parameters\DAO\ParametersDAO;
use \PsiFramework\Classes\Util\Parameters\DAO\ActionMappingDAO;
use \PsiFramework\Classes\Util\Parameters\Action\ActionMapping;
use \PsiFramework\Classes\Util\Parameters\ApplicationsParameters;
use \PsiFramework\Classes\Util\Parameters\DatabaseParameters;
use \PsiFramework\Classes\Util\Parameters\ObjectParameters;
use \PsiFramework\Classes\Util\Manager\ParametersManager;
use \PsiFramework\Classes\Util\HTTP\Session;
use \PsiFramework\Classes\Util\Log\Logger;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The ParametersManager class is the class to manage Parameters
 *
 * @package Manager
 */
class ParametersManager
{

    protected static /*ParametersManager*/ $_singleton = null ;
    
    protected /*ApplicationsParameters*/   $_applicationsParameters ;
    protected /*DatabaseParameters*/       $_databaseParameters ;
    protected /*ObjectParameters[]*/       $_objectParameters ;

    protected function __construct()
    {
        $parametersDao = ParametersDAO::getInstance();
        $this->_applicationsParameters
            = new ApplicationsParameters(
                $parametersDao->read(
                    ParametersDAO::getDocumentRoot(
                        getConfigFile('applications.xml')
                    )
                )
            );
        $this->_databaseParameters
            = new DatabaseParameters(
                $parametersDao->read(
                    ParametersDAO::getDocumentRoot(
                        getConfigFile('database.xml')
                    )
                )
            );

    }

    public function getApplicationsParameters()
    {
        return $this->_applicationsParameters ;
    }

    public function getDatabaseParameters()
    {
        return $this->_databaseParameters ;
    }

    public static function getInstance()
    {
        $_logger = new Logger('ParametersManager');
        if (self::$_singleton==null) {
            $session = Session::getInstance();
            if ($session->get('parameters') != null) {
                $_logger->debug('Retrieve parameters from session');
                self::$_singleton = $session->get('parameters');
            } else {
                $_logger->debug('Retrieve parameters from files');
                self::$_singleton = new ParametersManager();
                $session->set('parameters', self::$_singleton);
            }
        }

        return self::$_singleton;
    }
}
